<?php

namespace app\common\model;

use think\Model;

class Admin extends Model
{
    public function getUser($where = [])
    {
        $where['a.status'] = 1;
        return db('admin')
            ->alias('a')
            ->field('a.admin_id,a.username,r.role_name')
            ->join('__ADMIN_ROLE__ ar','a.admin_id = ar.admin_id','LEFT')
            ->join('__ROLE__ r','ar.role_id = r.role_id','LEFT')
            ->where($where)
            ->select();
    }

    public function getUserList($where = [],$field=[],$page= 0,$limit=10,$order=[])
    {
        $where['a.status'] = 1;
        if($page){
            return db('admin')
                ->alias('a')
                ->field($field)
                ->join('admin_role ar','a.admin_id = ar.admin_id','LEFT')
                ->join('role r','ar.role_id = r.role_id','LEFT')
                ->join("admin_info ai","ai.admin_id = a.admin_id")
                ->where($where)
                ->page($page,$limit)
                ->order($order)
                ->select();
        } else {
            return db('admin')
                ->field($field)
                ->alias('a')
                ->join('admin_role ar','a.admin_id = ar.admin_id','LEFT')
                ->join('role r','ar.role_id = r.role_id','LEFT')
                ->join("admin_info ai","ai.admin_id = a.admin_id")
                ->where($where)
                ->count();
        }
    }
    public function geUserInfo($where,$field=[]){
        return db('admin')
            ->alias('a')
            ->field($field)
            ->join('admin_role ar','a.admin_id = ar.admin_id','LEFT')
            ->join('role r','ar.role_id = r.role_id','LEFT')
            ->join("admin_info ai","ai.admin_id = a.admin_id")
            ->join("area a1","a1.area_id = ai.area_id",'LEFT')
            ->join("area a2","a2.area_id = a1.pid",'LEFT')
            ->join("area a3","a3.area_id = a2.pid",'LEFT')
            ->where($where)
            ->find();
    }

}